package com.example.a22310.myapplication.ViewModel.User;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.example.a22310.myapplication.Activity.LoginActivity;
import com.example.a22310.myapplication.Beans.user;
import com.example.a22310.myapplication.Listener.UserListener;
import com.example.a22310.myapplication.Model.UserModel;
import com.example.a22310.myapplication.databinding.ActivityRegisterBinding;

public class RegisterViewModel implements UserListener {
    private ActivityRegisterBinding activityRegisterBinding;
    private AppCompatActivity activity;
    private UserModel user;

    public RegisterViewModel(AppCompatActivity activity, ActivityRegisterBinding activityRegisterBinding){
        this.activity = activity;
        this.activityRegisterBinding = activityRegisterBinding;
        activityRegisterBinding.setModel(this);
    }

    public void onback(View view) {  //直接返回登录页面
        activity.onBackPressed();
    }

    public void button_commit_register(View view) {  //注册
        user=new UserModel();
        user.setUser_user(activityRegisterBinding.phoneNumber.getText().toString());
        user.setUser_username(activityRegisterBinding.edtUsername.getText().toString());
        user.setUser_password(activityRegisterBinding.edtPwd.getText().toString());
        UserModel userModel=new UserModel();
        userModel.register(user,RegisterViewModel.this);
    }

    @Override
    public void onSuccess(com.example.a22310.myapplication.Beans.user user) {

    }

    @Override
    public void onSuccess(UserModel user) {
        if(!"0".equals(user.getSuccess()))
        {
            Intent intent = new Intent();
            intent.putExtra("phone", activityRegisterBinding.phoneNumber.getText().toString());
            intent.putExtra("pwd", activityRegisterBinding.edtPwd.getText().toString());
            intent.setClass(activity, LoginActivity.class);
            activity.startActivity(intent);
            activity.finish();
            Toast.makeText(activity,"注册成功",Toast.LENGTH_LONG).show();
        }
        else
            Toast.makeText(activity,"请按格式输入",Toast.LENGTH_LONG).show();
    }



    @Override
    public void onFailure() {
        Toast.makeText(activity,"连接服务器失败",Toast.LENGTH_LONG).show();
    }
}
